# Author: Allen Hutchison
# Copyright (C) 2006 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#      Unless required by applicable law or agreed to in writing, software
#      distributed under the License is distributed on an "AS IS" BASIS,
#      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#      See the License for the specific language governing permissions and
#      limitations under the License.
#      
# Base package for the Open Automation Framework. All other modules in OAF
# inherit from this one.

package Oaf;
use base qw(Exporter);

use strict;
use warnings;

use Oaf::Config;

our $VERSION = '0.01';

# new()
#  Create a new instance of an OAF. Give it a config object. All other
#  Oaf classes use this new() method in their own new() methods.
sub new {
  my($class, @args) = @_;

  my $self = {};

  # Create a config object.
  $self->{'cfg'} = Oaf::Config->new(@args);

  bless $self, $class;

  return $self;
}

# test_passed()
#  This method is used to indicate that your
#  test passed.
sub test_passed {
  my($self, $message) = @_;

  $self->print_msg(-1, $message);
  exit 0;
}

# test_failed()
#  This method is used to indicate that your
#  test failed.
sub test_failed {
  my $self = shift;
  my $message = shift;

  $self->print_msg(-1, $message);
  exit 1;
}

# fatal_error()
#  Method to print a fatal error message
sub fatal_error {
  my($self, @args) = @_;
  $self->print_msg(0, "FATAL ERROR", @args);
}

# error()
#  Method to print an error message
sub error {
  my($self, @args) = @_;
  $self->print_msg(1, "ERROR", @args);
}

# warning()
#  Method to print a warning message
sub warning {
  my($self, @args) = @_;
  $self->print_msg(2, "WARNING", @args);
}

# info()
#  Method to print an info message
sub info {
  my($self, @args) = @_;
  $self->print_msg(3, "INFO", @args);
}

# debug()
#  Method to print a debug message
sub debug {
  my($self, @args) = @_;
  $self->print_msg(4, "DEBUG", @args);
}

# print_msg()
#  This is the method used by the print methods
#  above. You should use the abstracted methods
#  to print instead of this one.
#
# -1 = Test Status Messages
#  0 = Fatal Error Fail the Test
#  1 = Error that we can live with
#  2 = Warning
#  3 = Information
#  4 = Debug Messages
sub print_msg {
  my($self, $level, @args) = @_;

  my $current_level = $self->get_param('oaf', 'msg_level', 2);

  if ($level <= $current_level) {
    $self->_format_print_msg(@args);
  }

  if (0 == $level) {
    $self->test_failed();
  }
}

# _format_print_msg()
#  This method can be overridden to print however you want
#  inside OAF.
sub _format_print_msg {
  my($self, $prefix, @args) = @_;
  my $caller = get_caller();

  if (scalar(@args) > 0) {
    foreach (@args) {
      print "$prefix $caller -> $_\n";
    }
  } else {
    print "$prefix $caller\n";
  }
}

# get_caller()
#  Return the top of the caller stack
#  for debugging in the print calls.
sub get_caller {
  my $level = 1;
  my $lastcall;
  while (1) {
    my $call = (caller($level))[3];
    if (defined($call)) {
      $lastcall = $call;
      $level++;
    } else {
      return $lastcall;
    }
  }
}

# time_stamp()
#  Return a custom time stamp, for logs
sub time_stamp {
  my($self, $time) = @_;

  $time = time unless defined($time);

  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
  $year += 1900;
  $mon += 1;

  $mon = sprintf("%02d", $mon);
  $mday = sprintf("%02d", $mday);

  $hour = sprintf("%02d", $hour);
  $min = sprintf("%02d", $min);
  $sec = sprintf("%02d", $sec);

  return "$year-$mon-$mday $hour:$min:$sec";
}

# set_param()
#  This is a wrapper for the set_param method in the
#  config object.
sub set_param {
  my($self, @args) = @_;
  unless ($args[1] eq 'msg_level') {
    $self->debug("Setting parameter: @args");
  }
  return $self->{'cfg'}->set_param(@args);
}

# get_param()
#  This is a wrapper for the get_param method in the
#  config object.
sub get_param {
  my($self, @args) = @_;
  unless ($args[1] eq 'msg_level') {
    $self->debug("Getting parameter: @args");
  }
  return $self->{'cfg'}->get_param(@args);
}

# Have to Eval to something
1;
__END__
